package com.sphd.beautysalon.model;


import com.android.volley.VolleyError;
import com.sphd.beautysalon.info.SalesInfo;
import com.sphd.beautysalon.presenter.StateAPresenterImpl;
import com.sphd.beautysalon.interfaces.VolleyJsonObjectCallBack;
import com.sphd.beautysalon.interfaces.VolleyStringCallBack;
import com.wzh.frame.utils.JsonUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;
import java.util.Map;

/**
* Created by Hu on 2016/12/08
*/

public class StateAModelImpl implements StateAModel, VolleyJsonObjectCallBack {
    StateAPresenterImpl stateAPresenter;
    @Override
    public void senUrl(String url, StateAPresenterImpl stateAPresenter) {
        this.stateAPresenter=stateAPresenter;
        volleyUtil.setJsonObjectRequest(url,this);
    }

    @Override
    public void recUrlMap(String s, Map<String, String> map, StateAPresenterImpl stateAPresenter) {
        this.stateAPresenter=stateAPresenter;
        volleyUtil.setStringRequest(s, new VolleyStringCallBack() {
            @Override
            public void onResponse(String response) {
               int res=JsonUtil.getJsonObject(response).getIntValue("res");
                stateAPresenter.recRes(res);

            }

            @Override
            public void onErrorResponse(VolleyError error) {
            stateAPresenter.sendError(error);
            }
        }, map);
    }

    @Override
    public void sendCancleUrl(String s, StateAPresenterImpl stateAPresenter) {
        this.stateAPresenter=stateAPresenter;
        volleyUtil.setJsonObjectRequest(s, new VolleyJsonObjectCallBack() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    int status=response.getInt("status");
                    stateAPresenter.recCanRes(status);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                stateAPresenter.sendError(error);
            }
        });
    }

    @Override
    public void onResponse(JSONObject response) {
        try {
            int status0sum=response.getInt("status0sum");
            int status1sum=response.getInt("status1sum");
            int status2sum=response.getInt("status2sum");
            int status3sum=response.getInt("status3sum");
            int status6sum=response.getInt("status6sum");
            List<SalesInfo> salesInfos = JsonUtil.getListFromString(response.getString("res"),SalesInfo.class);
            stateAPresenter.sendResAndList(status0sum,status1sum,status2sum,status3sum,status6sum,salesInfos);

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        stateAPresenter.sendError(error);
    }
}